package com.herch.system.service.impl;
import com.herch.common.constant.MnlConstans;
import com.herch.common.utils.CommonUtil;
import com.herch.system.domain.HqEquipmentInfo;
import com.herch.system.domain.HqMonitorData;
import com.herch.system.domain.HqMonitorDataIntermediate;
import com.herch.system.domain.HqParamSet;
import com.herch.system.mapper.*;
import com.herch.system.service.IRS485DataAcquisitionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@DependsOn("springContextUtil")
public class RS485DataAcquisitionServiceImpl implements IRS485DataAcquisitionService {
	@Autowired
	private HqEquipmentInfoMapper equipmentInfoMapper;
	@Autowired
	private HqParamSetMapper paramSetMapper;
	@Autowired
	private HqMonitorDataIntermediateMapper monitorDataIntermediateMapper;
	@Autowired
	private SimpMessagingTemplate simpMessagingTemplate;
	@Override
	public void parseRS485ReceivedData(String inputString)  {
		Map<String,Object> map = new HashMap<>();
		String receiveTime = CommonUtil.getSimpleFormatTimestamp();
		// 地址编码
		String address = Integer.valueOf(inputString.substring(0, 2), 16).toString();
		// 通道数量
		int channelNum = Integer.valueOf(inputString.substring(4, 6), 16) / 4;
		map.put("equipmentAddr", address);
		map.put("flag", 0);
		HqEquipmentInfo equipmentInfo = equipmentInfoMapper.selectEquipmentInfoByAddr(map);
		List<HqParamSet> paramSetList =  paramSetMapper.selectHqParamSetList();
		String minTem = paramSetList.get(0).getThresholdValue();
		String maxTem = paramSetList.get(1).getThresholdValue();
		map.put("rs485ReceiveData",equipmentInfo.getEquipmentName()+"<<<"+inputString);
		simpMessagingTemplate.convertAndSend( "/data/task", map);
		// 去掉前六个标识性字符，真正的有效字符是从第七个开始
		inputString = inputString.substring(6);
		if(equipmentInfo != null){
			if(channelNum == equipmentInfo.getNumOfCh()){
				for (int i = 0; i < channelNum; i++) {
					HqMonitorDataIntermediate monitorData = new HqMonitorDataIntermediate();
					monitorData.setEquipmentId(equipmentInfo.getEquipmentId());
					monitorData.setEquipmentSize(equipmentInfo.getEquipmentSize());
					monitorData.setEquipmentName(equipmentInfo.getEquipmentName());
					monitorData.setNumOfCh(equipmentInfo.getNumOfCh());
					monitorData.setEquipmentAddr(address);
					monitorData.setChannelNum("CH"+(i+1));
					String temStr = (StringUtils.substring(inputString,i * 8, i * 8 + 4).trim()).toLowerCase();
					setTemDataByT(minTem, maxTem, monitorData,temStr);
					Integer pd = Integer.valueOf((StringUtils.substring(inputString,i * 8 + 4, i * 8 + 6)).trim(), 16);
					Integer uv = Integer.valueOf((StringUtils.substring(inputString,i * 8 + 6, i * 8 + 8)).trim(), 16);
					monitorData.setPd(String.valueOf(pd));
					monitorData.setUv(String.valueOf(uv));
					monitorData.setCollectTime(receiveTime);
					monitorDataIntermediateMapper.insertHqMonitorDataIntermediate(monitorData);
				}
			}
		}
	}

	private void setTemDataByT(String minTem, String maxTem, HqMonitorDataIntermediate monitorData, String temStr) {
		switch (temStr) {
			case MnlConstans.B7530:
				// 光纤故障
				monitorData.setTemState(2);
				monitorData.setStateName(MnlConstans.FIBER_FAILURE);
				monitorData.setTemperature(MnlConstans.TEM_3000);
				break;
			case MnlConstans.Bf43e:
			case MnlConstans.Bfed3:
				// 光纤故障
				monitorData.setTemState(2);
				monitorData.setStateName(MnlConstans.FIBER_FAILURE);
				monitorData.setTemperature(MnlConstans.TEM_MINUS301);
				break;
			case MnlConstans.Beeee:
				// 测温仪故障
				monitorData.setTemState(1);
				monitorData.setStateName(MnlConstans.THERMOMETER_FAILURE);
				monitorData.setTemperature(MnlConstans.TEM_MINUS437);
				break;
			case MnlConstans.Bf434:
				// 测温仪故障
				monitorData.setTemState(1);
				monitorData.setStateName(MnlConstans.THERMOMETER_FAILURE);
				monitorData.setTemperature(MnlConstans.TEM_MINUS302);
				break;
			case MnlConstans.B752f:
				// 系统调整中
				monitorData.setTemState(4);
				monitorData.setStateName(MnlConstans.SYSTEM_ADJUST);
				monitorData.setTemperature(MnlConstans.TEM_2999);
				break;
			case MnlConstans.Bf448:
				// 系统调整中
				monitorData.setTemState(4);
				monitorData.setStateName(MnlConstans.SYSTEM_ADJUST);
				monitorData.setTemperature(MnlConstans.TEM_MINUS300);
				break;
			default:
				int ret = Integer.parseInt(temStr, 16);
				ret = ((ret & 0x8000) > 0) ? (ret - 0x10000) : (ret);
				Float temp = ret / 100F;
				if (temp < Float.valueOf(minTem)) {
					monitorData.setTemState(10);
					monitorData.setStateName(MnlConstans.LOWTEM_FAILURE);
				} else if (temp > Float.valueOf(maxTem)) {
					monitorData.setTemState(9);
					monitorData.setStateName(MnlConstans.HIGHTEM_FAILURE);
				} else {
					monitorData.setTemState(5);
					monitorData.setStateName(MnlConstans.TEM_NORMAL);
				}
				monitorData.setTemperature(temp.toString());
				break;
		}
	}
}
